<?php

class Notifikasi extends Controller {
	
	function index() {
            $this->set('title','YOU - Profile');
            global $session;
            global $imgedit;
            $user_id = $session->get('userid');
            $result = $this->NotifikasiModel->query('SELECT * FROM user WHERE idUser=\''.mysql_real_escape_string($user_id).'\'');
            $nama = $result[0]['User']['fullname'];
            $birthday = $result[0]['User']['tgllahir'];
            $email = $result[0]['User']['email'];
            $gender = $result[0]['User']['gender'];
            $location = $result[0]['User']['kota'];
            $profpic = $result[0]['User']['pp'];
            $imgedit->load($profpic);
            $imgedit->scale(50);
            $img = BASE_PATH.'/'.$this->NotifikasiModel->getFilename($profpic).'_thumb'.$this->NotifikasiModel->getExtension($profpic);
            $imgedit->save($this->NotifikasiModel->getFilename($profpic).'_thumb'.$this->NotifikasiModel->getExtension($profpic));
            $this->set('user',$nama);
            $this->set('pbirthday',$birthday);
            $this->set('pemail',$email);
            $this->set('pgender',$gender);
            $this->set('plocation',$location);
            $this->set('pimg',$img);
            $myid = $this->session->get('userid');
            $result = $this->NotifikasiModel->getWhoOnline($myid);
            $this->set('online',$result);
	}

        function notif(){
            global $session;
            $user_id = $session->get('userid');
            $result = $this->NotifikasiModel->query('SELECT * FROM wall WHERE iduser=\''.mysql_real_escape_string($user_id).'\'');
            $idwall = $result[0]['Wall']['idwall'];
            $this->set('idwall', $idwall);
            $res2 = $this->NotifikasiModel->query('SELECT * FROM notifikasi WHERE p1=\''.mysql_real_escape_string($user_id).'\' ORDER BY time DESC');
            $hasil = array();
            $i = 0;
            foreach ($res2 as $ress){
                $hasil[$i] = $this->NotifikasiModel->query('SELECT fullname FROM user WHERE iduser=\''.mysql_real_escape_string($ress['Notifikasi']['p2']).'\'');
                $i++;
            }
            $this->set('notifdata',$res2);
            $this->set('notif',$hasil);
            $this->_render_header = false;
        }

	function beforeAction(){
		$this->set('title',null);
	}

        function insert(){
            $msg = $_POST['msg'];
            $receiver = $_POST['chatto'];
            $to = $this->NotifikasiModel->getUserId($receiver);
            $from = $this->session->get('userid');
            $return = $this->NotifikasiModel->insert($from,$to,$msg);
            $this->set('return',$return);
            $this->_render_header = false;
        }
        function fetch($to){
            $from = $this->session->get('userid');
            $to = $this->NotifikasiModel->getUserId($to);
            $result = $this->NotifikasiModel->fetch($from,$to);
            $image = array();
            $image['from'] = $this->NotifikasiModel->getProfPic($from);
            $image['to'] = $this->NotifikasiModel->getProfPic($to);
            $chatid = array();
            $chatid['to'] = $this->NotifikasiModel->getFullName($to);
            $chatid['from'] = $this->NotifikasiModel->getFullName($from);
            $this->set('chatid',$chatid);
            $this->set('chatp',$image);
            $this->set('chatmessage',$result);
            $this->_render_header = false;
        }
        function loadconv($to){
            $this->set('to',$to);
            $this->_render_header = false;
        }

}
